package com.ruoqing.core.web;

import lombok.SneakyThrows;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import java.beans.PropertyEditorSupport;
import java.util.Date;

/**
	* @author YaoXian
	* @title: BaseRestApi
	* @projectName ruoxi-blog
	* @description: 若兮微服务 - 控制层基类(所有控制层皆可继承)
	* @date 2020-12-10 18:33
	*/
public class BaseRestApi {
		
		/**
			* 将前台传递过来的日期格式的字符串，自动转化为Date类型
			*/
		@InitBinder
		public void initBinder(WebDataBinder binder)
		{
				// Date 类型转换
				binder.registerCustomEditor(Date.class, new PropertyEditorSupport()
				{
						@SneakyThrows
						@Override
						public void setAsText(String text)
						{
								setValue(DateUtils.parseDate(text));
						}
				});
		}
}
